<template>
  <el-dialog title="删除" :before-close="onCancel" width="400px" :visible.sync="visible">
    确认要删除{{ name }}
    <template #footer>
      <el-button @click="onCancel">取 消</el-button>
      <el-button type="primary" @click="onConfirm" :loading="loading">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {removePageTab} from "@/utils/page-tab-util";

export default {
  props: ["id", "name"],
  name: "del-dialog",
  data() {
    return {
      visible: false,
      loading: false,
    }
  },
  methods: {
    onConfirm() {
      this.id != null ? this.handleDelAccessories() : this.$message.error('备件id不存在')
    },
    onCancel() {
      this.visible = false
    },
    async handleDelAccessories() {
      this.loading = true
      try {
        const res = (await this.$http.delete(`/part_categories/${this.id}`)).data
        res.success ? this.$message.success(res.msg) : this.$message.error(res.msg)
        this.loading = false
        this.onCancel()
        const {lastPath} = await removePageTab('/bom/accessories/info')
        await this.$router.push(lastPath || '/');
      } catch (err) {
        new Error(err)
      }
    }
  }
}
</script>
<style scoped>

</style>
